package com.hik.controller;
import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hik.Service.UserService;
import com.hik.entity.User;
import com.hik.utils.Base64Utils;
import com.hik.utils.DateUtils;
import com.hik.utils.EncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "账号接口")
@RequestMapping("user")
@RestController
public class UserController {
    @Resource
    private UserService userService;
    @ApiOperation("新增修改")
    @PutMapping("saveUpdate")
    public String saveUpdate(@ApiParam("user实体类")@RequestBody User user) throws Exception {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",user.getName());
        User user2=userService.getOne(queryWrapper);
        String str=user.getPassword();
        user.setPassword(Base64Utils.jiaM(str));
        String jmStartDate=EncryptUtils.encryptDES(user.getStartTime());
        String jmEndDate=EncryptUtils.encryptDES(user.getEndTime());
        user.setStartTime(jmStartDate);
        user.setEndTime(jmEndDate);
        if(StringUtils.isNotBlank(user.getId())){
            //说明不为空，是修改
            if(user2!=null&&!user2.getId().equals(user.getId())){
                return "账号已存在";
            }
            userService.updateById(user);
            return "修改成功";
        }else{
            if(user2 != null){
                return "账号已存在";
            }
            String id=UUID.randomUUID().toString().replace("-","");
            user.setId(id);
            userService.save(user);
            return "新增成功";
        }
    }
    @ApiOperation("验证")
    @PostMapping("getUser")
    public String getUser(@ApiParam("账号")@RequestParam("name")String name,
                              @ApiParam("密码")@RequestParam("password")String password) throws Exception {
        HashMap<String,Object> hashMap=new HashMap<>();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        User user=userService.getOne(queryWrapper);
        if(user==null){
            hashMap.put("code","303");
            hashMap.put("msg","账号不存在");
        }else{
            String startDate=EncryptUtils.decryptDES(user.getStartTime());
            String endDate=EncryptUtils.decryptDES(user.getEndTime());
            String str=Base64Utils.jiaM(password);
            if(str.equals(user.getPassword())){
                Date date=new Date();
                Date date1= DateUtils.stringToDate(startDate);
                Date date2=DateUtils.stringToDate(endDate);
                if(date.after(date1)&&date.before(date2)){
                    hashMap.put("code","200");
                    hashMap.put("msg","登录成功");
                    hashMap.put("data",endDate);
                }else{
                    hashMap.put("code","303");
                    hashMap.put("msg","账号失效");
                }
            }else{
                hashMap.put("code","303");
                hashMap.put("msg","密码错误");
            }
        }
      String str=JSONObject.toJSONString(hashMap);
        return str;
    }
    @ApiOperation("查询账号")
    @GetMapping("selectUser")
    public List<User>  selectUser(@ApiParam("输入账号,不输入为查询所有")@RequestParam("name")String name){
        List<User> list=new ArrayList<> ();
        if(StringUtils.isEmpty(name)){
          list=userService.list();
        }else{
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("name",name);
            list =userService.list(queryWrapper);
        }
        return list;
    }
    @ApiOperation("生成token，给客户部署时这个接口删除掉")
    @GetMapping("getToken")
    public String getToken(@ApiParam("账号")@RequestParam("name") String name,
                           @ApiParam("到期时间:2020-04-01 00:00:00")@RequestParam("date")String date){
        HashMap<String,String> map=new HashMap<>();
        map.put("name",name);
        map.put("date",date);
        String str=null;
        try {
           str= EncryptUtils.encryptDES(JSONObject.toJSONString(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
    @ApiOperation("放入token,修改有效时间")
    @GetMapping("token")
    public String updateToken(@ApiParam("token")@RequestParam("token") String token) throws Exception {
        HashMap<String,String> r=new HashMap<>();
        String string=null;
        try {
            string=EncryptUtils.decryptDES(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,String> map= JSONObject.parseObject(string,HashMap.class);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",map.get("name"));
        User user=userService.getOne(queryWrapper);
        Date date=DateUtils.stringToDate(map.get("date"));
        if(user==null||date.before(new Date())){
            r.put("code","303");
            r.put("msg","无效密令");
        }else{
            String jmEndDate=EncryptUtils.encryptDES(map.get("date"));
            user.setEndTime(jmEndDate);
            userService.updateById(user);
            r.put("code","200");
            r.put("msg","成功获得权限");
        }
        String str=JSONObject.toJSONString(r);
        return str;
    }
}
